let request = require('request');
const urlUtil = require('../../util/urlUtil');

const gBase = 'http://www.huli667.com:81/sms/api/';
const gUrl = {
    login: gBase + 'login',
    getPhone: gBase + 'getPhone',
    getMessage: gBase + 'getMessage',
    userinfo: gBase + 'userinfo',
    cancelAllRecv: gBase + 'cancelRecv',
    addBlacklist: gBase + 'addBlacklist'
}
const gConfig = {
    user: "api-fooalCBW",
    pass: "a456123",
    smsSid: "266"
}

// 设置代理，忽略证书检查、自动重定向
let option = {
    // proxy: 'http://127.0.0.1:8888',
    rejectUnauthorized: false,
    followRedirect: false,
    encoding: null,
};

function login() {
    let param = {
        username: gConfig.user,
        password: gConfig.pass
    };
    return new Promise((resolve, reject) => {
        request.get(gUrl.login + urlUtil.getUrlParam(param),
            option,
            function (err, response, body) {
                if (!err && response.statusCode == 200) {
                    console.log('res ok: ' + body);
                    resolve(JSON.parse(body));
                } else {
                    console.log('res err: ' + err);
                    reject(err);
                }
            }
        );
    });
}

function getPhone(token, phone) {
    let param = phone ? {
        token: token,
        sid: gConfig.smsSid,
        phone: phone
    } : {
            token: token,
            sid: gConfig.smsSid,
        };
    return new Promise((resolve, reject) => {
        request.get(gUrl.getPhone + urlUtil.getUrlParam(param),
            option,
            function (err, response, body) {
                if (!err && response.statusCode == 200) {
                    console.log('res ok: ' + body);
                    resolve(JSON.parse(body));
                } else {
                    console.log('res err: ' + err);
                    reject(err);
                }
            }
        );
    });
}

function getMessage(token, phone) {
    let param = {
        token: token,
        sid: gConfig.smsSid,
        phone: phone
    };
    return new Promise((resolve, reject) => {
        request.get(gUrl.getMessage + urlUtil.getUrlParam(param),
            option,
            function (err, response, body) {
                if (!err && response.statusCode == 200) {
                    console.log('res ok: ' + body);
                    resolve(JSON.parse(body));
                } else {
                    console.log('res err: ' + err);
                    reject(err);
                }
            }
        );
    });
}

function userinfo(token) {
    let param = {
        token: token,
    };
    return new Promise((resolve, reject) => {
        request.get(gUrl.userinfo + urlUtil.getUrlParam(param),
            option,
            function (err, response, body) {
                if (!err && response.statusCode == 200) {
                    console.log('res ok: ' + body);
                    resolve(JSON.parse(body));
                } else {
                    console.log('res err: ' + err);
                    reject(err);
                }
            }
        );
    });
}

function cancelAllRecv(token) {
    let param = {
        token: token,
        sid: gConfig.smsSid,
        phone: 'all'
    };
    return new Promise((resolve, reject) => {
        request.get(gUrl.cancelAllRecv + urlUtil.getUrlParam(param),
            option,
            function (err, response, body) {
                if (!err && response.statusCode == 200) {
                    console.log('res ok: ' + body);
                    resolve(JSON.parse(body));
                } else {
                    console.log('res err: ' + err);
                    reject(err);
                }
            }
        );
    });
}

function addBlacklist(token, phone) {
    let param = {
        token: token,
        sid: gConfig.smsSid,
        phone: phone
    };
    return new Promise((resolve, reject) => {
        request.get(gUrl.addBlacklist + urlUtil.getUrlParam(param),
            option,
            function (err, response, body) {
                if (!err && response.statusCode == 200) {
                    console.log('res ok: ' + body);
                    resolve(JSON.parse(body));
                } else {
                    console.log('res err: ' + err);
                    reject(err);
                }
            }
        );
    });
}

module.exports = {
    login,
    getPhone,
    getMessage,
    userinfo,
    cancelAllRecv,
    addBlacklist
}